package com.gilight.base.controller;


import com.gilight.base.admin.product.AdminProductService;
import com.gilight.base.base.ResultEntity;
import com.gilight.base.base.exception.ServiceException;

import com.gilight.base.dto.goods.AdminSpuDTO;

import com.gilight.base.model.SysUser;
import com.gilight.base.service.SysUserZzjgService;
import com.gilight.base.util.MapUtils;
import com.gilight.base.util.ShiroUtil;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * @author liken
 * 商品管理控制类
 */
@RestController
@RequestMapping("/web/admin-product")
public class AdminProductController {

    @Autowired
    private AdminProductService adminProductService;
    @Autowired
    private SysUserZzjgService sysUserZzjgService;
    /**
     * 后台低频接口，无需缓存，用于选择商品，需要
     * @param jigouId
     * @param adminId
     * @return
     */
    @ApiOperation(value = "商品分类大树")
    @RequestMapping(value = "/spu-big-tree", method = {RequestMethod.GET})
    public ResultEntity getSpuBigTree(@RequestParam(required = false) Long jigouId,Long adminId) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setData(adminProductService.getSpuBigTree(adminId));
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        return resultEntity;
    }
    /**
     * 后台低频接口，无需缓存，用于选择商品，需要
     * @param zzjgId
     * @param adminId
     * @return
     */
    @ApiOperation(value = "商品分类大树")
    @RequestMapping(value = "/spu-big-tree-cold-drind", method = {RequestMethod.GET})
    public ResultEntity getSpuBigTreeColdDrind(@RequestParam(required = false) Long zzjgId,Long adminId) {
        ResultEntity resultEntity = new ResultEntity();
        List<Map<String,Object>> zzjgsUser = sysUserZzjgService.getCurrentUserZzjgs();
        if(zzjgId == null){
            zzjgId = 1L;
            if (zzjgsUser != null && zzjgsUser.size() > 0) {
                zzjgId =  MapUtils.getLong(zzjgsUser.get(0), "zzjgId");
            }
        }

        try {
            resultEntity.setData(adminProductService.getSpuBigTreeColdDrind(adminId,zzjgId));
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        return resultEntity;
    }
    @ApiOperation(value = "创建")
    @RequestMapping(value = "/create")
    public ResultEntity create(@RequestBody AdminSpuDTO spuDTO, Long adminId,Integer isBaiHuo) {
        ResultEntity resultEntity = new ResultEntity();
        List<Map<String,Object>> zzjgsUser = sysUserZzjgService.getCurrentUserZzjgs();
        try {
            if(zzjgsUser!=null&&zzjgsUser.size()>0){
                Long zId = MapUtils.getLong(zzjgsUser.get(0),"zzjgId");
                spuDTO.setZzjgId(zId);
            }
            resultEntity.setData(adminProductService.create(spuDTO,adminId,isBaiHuo));
        } catch (ServiceException e) {
            e.printStackTrace();
        };
        return resultEntity;
    }

    /**
     * 商品列表
     * @param page
     * @param limit
     * @param categoryId
     * @param title
     * @param barcode
     * @param status
     * @param adminId
     * @return
     */
    @ApiOperation(value = "分页条件查询")
    @RequestMapping(value = "/page", method = {RequestMethod.GET})
    public ResultEntity list(
            Integer page,
            Integer limit,
            Long categoryId,
            String title,
            String barcode,
            Integer status,
            Long zzjgId,
            Long adminId) {
        ResultEntity resultEntity = new ResultEntity();

        try {
            List<Map<String,Object>> zzjgsUser = sysUserZzjgService.getCurrentUserZzjgs();
            Long zId = 1L;
            if(zzjgsUser!=null&&zzjgsUser.size()>0) {
                zId =  MapUtils.getLong(zzjgsUser.get(0), "zzjgId");
            }
            resultEntity.setData(adminProductService.list(page,limit,categoryId,title,barcode,status,zzjgId,adminId));
        } catch (ServiceException e) {
            e.printStackTrace();
        };
        return resultEntity;
    }




    @ApiOperation(value = "更新商品信息")
    @RequestMapping(value = "/edit", method = {RequestMethod.POST})
    public ResultEntity edit(
            @RequestBody  AdminSpuDTO spuDTO,
            Long adminId) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setData(adminProductService.edit(spuDTO,adminId));
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        return resultEntity;
    }

    @ApiOperation(value = "详情")
    @RequestMapping(value = "/detail")
    public ResultEntity detail(
            Long spuId,
            Long adminId,
            @RequestParam(defaultValue = "0",required = false) Integer isBaiHuo) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setData(adminProductService.detail(spuId,adminId,isBaiHuo));
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        return resultEntity;
    }

    @ApiOperation(value = "上下架")
    @RequestMapping(value = "/freeze-or-activation")
    public ResultEntity  freezeOrActivation(
            Long spuId,
            Integer status,
            Long adminId) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setData(adminProductService.freezeOrActivation(spuId,status,adminId));
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        return resultEntity;
    }
    @ApiOperation(value = "删除")
    @RequestMapping(value = "/delete")
    public ResultEntity  delete(
            Long spuId,
            Long adminId) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setData(adminProductService.delete(spuId,adminId));
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        return resultEntity;
    }
    @ApiOperation(value = "批量删除")
    @RequestMapping(value = "/batch-delete")
    public ResultEntity  batchDelete(
            String ids,
            Long adminId) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setData(adminProductService.batchDelete(ids,adminId));
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        return resultEntity;
    }
    @ApiOperation(value = "重建商品缓存")
    @RequestMapping(value = "/rebuild-product-cache")
    public ResultEntity  rebuildProductCache(
            Long adminId) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setData(adminProductService.rebuildProductCache(adminId));
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        return resultEntity;
    }

    /**
     * 该方法只在Redis中保存
     * @param spuId
     * @param num
     * @return
     */
    @ApiOperation(value = "商品限量")
    @RequestMapping(value = "/limit-product-spu-stock-cache")
    public ResultEntity  limitSpu(
            Long spuId,Integer num) {
        ResultEntity resultEntity = new ResultEntity();

        resultEntity.setData(adminProductService.limitSpu(spuId,num));

        return resultEntity;
    }
}
